const Carousel = require('../../models/carousel')
const Router = require('koa-router')
const koaJwt = require('koa-jwt')
const secretKey = require('../../config/keys').secretKey

const router = new Router()

/*
* @method GET
* @route /carousel/getCarousel
* @params none
* @description 获取当前走马灯列表
* */
router.get('/getCarousel', async ctx => {
  await Carousel.find().then(res => {
    console.log(res)
    ctx.status = 200
    ctx.body = res
  }).catch(err => {
    console.log(err)
    ctx.status = 500
    ctx.body = err
  })
})

/*
* @method POST
* @route /carousel/addCarousel
* @params img gid zhTitle enTitle
* @description 添加走马灯
* */
router.post('/addCarousel', koaJwt({ secret: secretKey }), async ctx => {
  const { body } = ctx.request
  const newCarousel = new Carousel({
    ...body
  })
  console.log(newCarousel)
  await newCarousel.save().then(res => {
    console.log(res)
    ctx.status = 200
    ctx.body = res
  }).catch(err => {
    console.log(err)
    ctx.status = 500
    ctx.body = res
  })
})

module.exports = router.routes()
